package com.weimob.hystrix.controller;

import java.util.List;
import java.util.concurrent.Future;

import org.apache.commons.lang.math.RandomUtils;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.google.common.collect.Lists;
import com.netflix.hystrix.contrib.javanica.annotation.HystrixCollapser;
import com.netflix.hystrix.contrib.javanica.annotation.HystrixCommand;

import lombok.AllArgsConstructor;
import lombok.Data;

@RestController
public class CollapserController {

	@GetMapping("/user")
	public User getUser(@RequestParam("username")String userName) {
		return new User(userName, RandomUtils.nextInt(), "TEST GET User");
	}

	@HystrixCollapser(collapserKey = "UserHystrixCollapser", batchMethod = "batchGetUser")
	@GetMapping("/singleUser")
	public Future<User> getSingleUser(@RequestParam("username") String userName) {
		return null;
//		return new User(userName, RandomUtils.nextInt(), "TEST GET Single User");
	}

	@HystrixCommand(commandKey = "batchGetUser")
	public List<User> batchGetUser(List<String> listUserName) {
		List<User> list = Lists.newArrayList();
		for (String userName : listUserName) {
			list.add(new User(userName, RandomUtils.nextInt(), "TEST GET Batch User"));
		}
		return list;
	}

	@Data
	@AllArgsConstructor
	private class User {

		String userName;
		Integer age;
		String remark;
	}
}
